package com.shf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shf.entity.Member;
import com.shf.entity.Orders;
import com.shf.entity.Product;
import com.shf.entity.Traveller;
import com.shf.mapper.*;
import com.shf.service.OrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 叶枫
 * 订单业务实现类
 */
@Service("os")
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
   private TravellerMapper travellerMapper;
    @Autowired
    private OrderTravellerMapper orderTravellerMapper;

    /**
     * 添加 order
     */
    @Override
    public void saveOrder(Orders orders) {
        orderMapper.saveOrder(orders);
        //获取 游客数组
        String[] travellerId = orders.getTravellerId();

        //没遍历一次调用 mapper层方法
        for (String travellerIds : travellerId) {

            memberMapper.addMmm(orders.getId(), travellerIds);
        }

    }

    /**
     * 显示所有订单
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Orders> findAllOrder(int page, int size) {
        PageHelper.startPage(page,size);

       List<Orders> orders = orderMapper.findAllOrder();

        PageInfo<Orders> pageInfo = new PageInfo<Orders>(orders);
        return pageInfo;

    }

    @Override
    public List<Product> selectProducts() {
        return  productMapper.findProducts();


    }

    @Override
    public List<Member> selectMembers() {

        return memberMapper.showMembers();
    }

    @Override
    public List<Traveller> selectTraveller() {
        return travellerMapper.selectTraveller();
    }

    /**
     * 显示订单详情
     */
    @Override
    public Orders findOrderDetail(String id) {
      return   orderMapper.findOrderDetail(id);
    }

    @Override
    public void deleteById(String id) {
        orderMapper.deleteById(id);
        orderTravellerMapper.deleteByOrderId(id);
    }

    @Override
    public void deleteManyOrder(String[] ids) {

        orderMapper.deleteManyOrder( ids);

        orderTravellerMapper.delByOrderId(ids);
    }

    @Override
    public Orders selectOrderById(String id) {

      return orderMapper.selectOrderById(id);


    }

    @Override
    public void editOrder(Orders orders) {

        orderMapper.editOrder(orders);
    }
}
